﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Common
{
    public static class Tools
    {
        /// <summary>
        /// 将不可显示字符替换为"."
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ConvertChar(string s)
        {
            char[] chars = s.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
            {
                if (Char.IsControl(chars[i]) || char.IsWhiteSpace(chars[i]))
                {
                    chars[i] = '.';
                }
            }
            return new string(chars);
        }

        /// <summary>
        /// 将给定的代码字节数组连接为一个数组
        /// </summary>
        /// <param name="data1"></param>
        /// <param name="data2"></param>
        /// <param name="data3"></param>
        /// <returns></returns>
        public static byte[] Contact(byte[] data1, byte[] data2, params byte[][] data3)
        {
            int length = 0;
            length = data1.Length + data2.Length;
            if (data3.Length > 0)
            {
                foreach (byte[] d in data3)
                {
                    length += d.Length;
                }
            }

            byte[] data = new byte[length];
            Array.Copy(data1, data, data1.Length);
            Array.Copy(data2, 0, data, data1.Length, data2.Length);

            int index = data1.Length + data2.Length;
            foreach (byte[] d in data3)
            {
                Array.Copy(d, 0, data, index, d.Length);
                index += d.Length;
            }
            return data;
        }

        /// <summary>
        /// 从指定字节数组中查找数据，返回是否有相同数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="length"></param>
        /// <param name="searchedData"></param>
        /// <returns></returns>
        public static bool Search(byte[] data, int length, byte[] searchedData)
        {
            bool searched = true;
            for (int i = 0; i < length - searchedData.Length; i++)
            {
                searched = true;
                for (int j = 0; j < searchedData.Length; j++)
                {
                    if (searchedData[j] != data[i + j])
                    {
                        searched = false;
                        break;
                    }
                }
                if (searched)
                {
                    return true;
                }
            }
            return searched;
        }

        /// <summary>
        /// 从指定字节数组中查找数据，返回相同数据出现的索引位置
        /// </summary>
        /// <param name="data"></param>
        /// <param name="length"></param>
        /// <param name="searchedData"></param>
        /// <returns>如果找到返回searchedData在data中的起始位置，否则返回-1</returns>
        public static int SearchIndex(byte[] data, int length, byte[] searchedData)
        {
            bool searched = true;
            for (int i = 0; i < length - searchedData.Length; i++)
            {
                searched = true;
                for (int j = 0; j < searchedData.Length; j++)
                {
                    if (searchedData[j] != data[i + j])
                    {
                        searched = false;
                        break;
                    }
                }
                if (searched)
                {
                    return i;
                }
            }
            return -1;
        }

        /// <summary>
        /// 比较字节数组是否相同
        /// </summary>
        /// <param name="data"></param>
        /// <param name="length"></param>
        /// <param name="targeData"></param>
        /// <returns></returns>
        public static bool Compare(byte[] data, byte[] targetData)
        {
            if (targetData.Length != data.Length)
            {
                return false;
            }

            for (int i = 0; i < data.Length; i++)
            {
                if (data[i] != targetData[i])
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// 从指定的字节数组中查找字符串，返回是否存在
        /// </summary>
        /// <param name="data"></param>
        /// <param name="length"></param>
        /// <param name="searchedData"></param>
        /// <returns></returns>
        public static bool Search(byte[] data, int length, string searchedData)
        {
            string s = UTF8Encoding.UTF8.GetString(data, 0, length);
            return s.IndexOf(searchedData) > -1;
        }

        /// <summary>
        /// 查找经验值数据
        /// </summary>
        /// <param name="datas">数据</param>
        /// <param name="length">长度</param>
        /// <returns>返回获得的经验值，如果没有找到经验值数据则返回-1</returns>
        public static int GetExp(byte[] datas, int length)
        {
            string tag = "获得经验值";
            string dataString = UTF8Encoding.UTF8.GetString(datas, 0, length);
            string expString = "";
            int index = dataString.IndexOf(tag);
            int temp;
            if (index > -1)
            {
                //expString= dataString.Substring(index + tag.Length, dataString.IndexOf('\'', index + tag.Length) - index - tag.Length);
                List<char> list = new List<char>();
                for (int i = index + tag.Length; i < length; i++)
                {
                    temp=(int)dataString[i];
                    if (temp > 47 && temp < 58)
                    {
                        list.Add(dataString[i]);
                    }
                    else
                    {
                        break;
                    }
                }

                char[] expChars = new char[list.Count];
                list.CopyTo(expChars);

                expString = new string(expChars);
                int.TryParse(expString, out index);
            }
            return index;
        }

        /// <summary>
        /// 打印异常信息到控制台
        /// </summary>
        /// <param name="ex"></param>
        public static void PrintException(Exception ex)
        {
            Console.WriteLine("---------------------------------------");
            Console.WriteLine(ex.Message);
            Console.WriteLine();
            Console.WriteLine(ex.StackTrace);
            Console.WriteLine("---------------------------------------");
        }
    }
}
